<?php 
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Download extends CI_Controller {
    public $territory;
    public function __construct() {
        parent::__construct();
        $this->load->library('session');
        $this->load->helper('directory');
        $this->load->helper('file');
        $this->load->helper('number');
        $this->load->model('mmaster');
        $this->load->model('mglobal');
        $this->load->model('mdownload');        
        
    }
    
    public function index(){
        $this->territory=$this->mdownload->getTerritory($this->session->userdata('username'));
        $report[]=$this->getReport('Monthly Sell In Report','Monthly Sell In','monthly_sell_in','sellin_monthly_report','month');
        $report[]=$this->getReport('Monthly Sell Out Report','Monthly Sell Out','monthly_sell_out','sellout_monthly_report','month');
        $report[]=$this->getReport('Daily Sell In Report','Daily Sell In ','daily_sell_in','sellin_report','date');
        
        $report[]=$this->getReport('Consignment Report','Daily Consignment ','daily_consignment','consignment_report','date');
         
        $report[]=$this->getReport('Agent List','Agent list Per ','agent','partner','date');       
        $report[]=$this->getReport('Outlet List','Outlet list Per ','outlet','partner','date');
        $report[]=$this->getReport('Outlet List','Outlet list Per ','outlet','partner','date');
        //$report[]=$this->getReport('Agent List','Agent List ','agent','partner');
        //$report[]=$this->getReport('Outlet List','Outlet List ','outlet','partner');
        $data['report']=$report;
        $this->load->view('vdownload',$data);
    }
    
    private function getReport($title, $prefix, $pattern, $directory, $type){
        
        $fileList=$this->getFileList($prefix, $pattern, $directory,$type);         
        $reportInfo['title']=$title;
        $i=0;
        foreach($fileList as $key=>$value){
            $reportInfo['listReport'][$i]['name']=$value['name'];
            $reportInfo['listReport'][$i]['label']=$value['label'];
            $reportInfo['listReport'][$i]['link']=$value['link'];
            $reportInfo['listReport'][$i++]['size']=$value['size'];
        }
        
        return $reportInfo;
    }
    
    private function getFileList($prefix, $pattern, $directory,$type){
        $dir=directory_map("./././file/download/$directory");
        if($this->territory['territory_type_name']=='Cluster'){
            $matcher=$pattern.'_'.$this->territory['region'].'_'.$this->territory['cluster'].'_';
        }else if ($this->territory['territory_type_name']=='Regional'){
        //CLEAN CLUSTER
            $matcher=$pattern.'_'.$this->territory['region'].'_.._';
            foreach($dir as $key => $value) {
                if (preg_match("/$matcher/", $value)) {
                    unset($dir[$key]);
                }
            }
            $matcher=$pattern.'_'.$this->territory['region'].'_';
        }else if ($this->territory['territory_type_name']=='Nasional'){
        //CLEAN REGION
            $matcher=$pattern.'_._';
            foreach($dir as $key => $value) {
                if (preg_match("/$matcher/", $value)) {
                    unset($dir[$key]);
                }
            }        
            
            $matcher=$pattern.'_';
        }else die('Access Denied');
        
        foreach($dir as $key => $value) {
            if (!preg_match("/^$matcher/", $value)) {
                unset($dir[$key]);
            }
        }
        array_multisort($dir,SORT_DESC,SORT_STRING);
        $i=0;
        if($type=='date'){
            $char=14;
            $char2=10;
        }
        else{
            $char=7;
            $char2=3;
        }
        foreach($dir as $key => $value) {
            $result[$i]['name']=$value ;
            $result[$i]['label']="$prefix ".substr($value,strlen($value)-$char,$char2);
            $result[$i]['link']=base_url()."file/download/$directory/".$value ;
            $info=get_file_info("file/download/$directory/".$value , array('size'));
            $result[$i++]['size']= byte_format($info['size']);
        }
        return $result;
    }
    
}